package base.dao;

import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserDao {

    public List<Map> getData (){
        List<Object> params = new ArrayList<Object>();
        Map data;
        Connection conn = null;
        List datas = new ArrayList();
        try {
            // 加
            Class.forName("com.mysql.jdbc.Driver");
            // 连
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/learn?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8","root","root");
            // 预
            String sql = "select * from user";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            // 执
            if(params!=null && params.size()>0){
                for(int i=0;i<params.size();i++){
                    preparedStatement.setObject(i+1, params.get(i));
                }
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            ResultSetMetaData resultSetMetaData = preparedStatement.getMetaData();
            int columnCount = resultSetMetaData.getColumnCount();
            while (resultSet.next()) {
                data = new HashMap<String, Object>();
                for (int i = 1; i < columnCount + 1; i++) {
                    data.put(resultSetMetaData.getColumnLabel(i), resultSet.getObject(resultSetMetaData.getColumnLabel(i)));
                }
                datas.add(data);
            }
            return datas;
            // 释
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
